/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.core.olddescriptors;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import net.miblounge.gradecalculator.exceptions.XMLException;

@XmlRootElement(namespace = "http://notenrechner.miblounge.net/")
@XmlType( 
 propOrder = { "name", "countryCode", "courseTypes", "examTypes" } 
) 
public class University {
	
	public static class Descriptor {
		private String id;
		private String name;
		
		public Descriptor()
		{
		}
		
		public Descriptor(final String id, final String name)
		{
			this.id = id;
			this.name = name;
		}

		public String getId() {
			return id;
		}
		public void setId(final String id) {
			this.id = id;
		}
		
		public String getName() {
			return name;
		}
		public void setName(final String name) {
			this.name = name;
		}
	}
	
	private String name;
	private String countryCode;
	private List<Descriptor> courseTypes;
	private List<Descriptor> examTypes;
	
	public University()
	{
		courseTypes = new ArrayList<Descriptor>();
		examTypes = new ArrayList<Descriptor>();
	}
	
	@XmlElement(name = "name")
	public String getName() {
		return name;
	}
	public void setName(final String name) {
		this.name = name;
	}
	
	@XmlElement(name = "countrycode")
	public String getCountryCode() {
		return countryCode;
	}
	public void setCountryCode(final String countryCode) {
		this.countryCode = countryCode;
	}
	
	@XmlElementWrapper(name="coursetypes")
	@XmlElement(name = "coursetype")
	public List<Descriptor> getCourseTypes() {
		return courseTypes;
	}
	public void setCourseTypes(final List<Descriptor> courseTypes) {
		this.courseTypes = courseTypes;
	}
	
	@XmlElementWrapper(name="examtypes")
	@XmlElement(name = "examtype")
	public List<Descriptor> getExamTypes() {
		return examTypes;
	}
	public void setExamTypes(final List<Descriptor> examTypes) {
		this.examTypes = examTypes;
	}
	
	public void addCourseType(final String id, final String name)
	{
		courseTypes.add(new Descriptor(id, name));
	}

	public void addExamType(final String id, final String name)
	{
		examTypes.add(new Descriptor(id, name));
	}

	public static University createFromXML(final String filename) {
		University result = null;
		Reader xmlinput = null;
		try {
			final JAXBContext context = JAXBContext.newInstance(University.class);
			final Unmarshaller unmarshaller = context.createUnmarshaller();
			xmlinput = new FileReader(filename);
			result = (University) unmarshaller.unmarshal(xmlinput);
		} catch (final JAXBException jaxbException) {
			throw new XMLException("Error reading university xml", jaxbException);
		} catch (final Exception e) {
			throw new XMLException("Error reading university xml", e);
		} finally {
			try {
				if (xmlinput != null) {
					xmlinput.close();
				}
			} catch (final IOException e) {
				throw new XMLException("Error closing university xml", e);
			}
		}
		return result;
	}

	public void saveXML(final String filename) {
		Writer xmloutput = null;
		try {
			final JAXBContext context = JAXBContext.newInstance(University.class);
			final Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			xmloutput = new FileWriter(filename);
			marshaller.marshal(this, xmloutput);
			xmloutput.flush();
		} catch (final JAXBException jaxbException) {
			throw new XMLException("Error writing university xml", jaxbException);
		} catch (final Exception e) {
			throw new XMLException("Error saving university xml", e);
		} finally {
			try {
				if (xmloutput != null) {
					xmloutput.close();
				}
			} catch (final Exception e) {
				throw new XMLException("Error closing university xml", e);
			}
		}
	}
}
